//
//  ViewController.swift
//  SnapKitDemo
//
//  Created by 李煜 on 2023/3/27.
//

import SnapKit//SnapKit (5.6.0)
import UIKit
//"will appear"
//"viewSafeAreaInsetsDidChange"
//"viewWillLayoutSubviews"
//"viewDidLayoutSubviews"
//"did appear"
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        safeAreaLayoutGuide()
        view.layoutMargins
    }
  
    //frame方式设置安全区域
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        debugPrint("viewSafeAreaInsetsDidChange")
        
        if #available(iOS 11.0, *) {
            let yellow = UIView()
            yellow.backgroundColor = randomColor()
            yellow.frame = CGRect.init(x: 0, y: self.view.safeAreaInsets.top, width: self.view.frame.size.width, height: self.view.frame.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom)
            view.addSubview(yellow)
        } else {
            // Fallback on earlier versions
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        debugPrint("will appear")
    }
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        debugPrint("viewWillLayoutSubviews")

    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        debugPrint("viewDidLayoutSubviews")
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        debugPrint("did appear")
    }
    
    
    
    
    
    
    //2.autolayout 方式设置安全区域
    //safeAreaLayoutGuide方式只能配合Auto Layout相对布局使用.frame对应safeAreaInsets方式设置.
    func safeAreaLayoutGuide() {
        let box = boxView()
        view.addSubview(box)
        box.snp.makeConstraints { make in
            make.edges.equalTo(self.view.safeAreaLayoutGuide)
//          同上  make.edges.equalTo(self.view.safeAreaLayoutGuide).inset(UIEdgeInsets(top: 5, left: 10, bottom: 3, right: 1))
//            make.edges.equalTo(view.safeAreaLayoutGuide)
        }
    }
  
}
extension ViewController {
  //创建一个随机颜色的视图
  func boxView() -> UIView {
    let a = UIView()
    a.backgroundColor = randomColor()
    return a
  }
  func containerView() ->UIView {
    let a = UIView()
    a.backgroundColor = .white
    a.layer.borderColor = UIColor.orange.cgColor
    a.layer.borderWidth = 1
    return a
  }
  // MARK: - 随机色 新API
  func randomColor()->UIColor {
    let r = CGFloat.random(in: 0...1)
    let g = CGFloat.random(in: 0...1)
    let b = CGFloat.random(in: 0...1)
    return UIColor(red: r, green: g, blue: b, alpha: 1)
  }
}
